package level_ajax_query.servlet;

import com.google.gson.Gson;
import level_ajax_query.dao.ProvinceDao;
import level_ajax_query.entry.Province;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/*
 * @Author liu-miss
 * @Description 连表的级联查询
 * @Date 19:20 2021/5/11
 **/
public class LevelQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json="{}";
        // 创建Dao层对象
        ProvinceDao provinceDao=new ProvinceDao();

        // 获取查询结果
        List<Province> province = provinceDao.findProvince();
        System.out.println("---------------------------");
        // 转换成json数据
        if (province!=null){
            // 调用jackson工具库，实现list---json
            Gson object=new Gson();
            json=object.toJson(province);
            System.out.println(province);
        }
        // 输出json，响应ajax请求，返回数据
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.println(json);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
